package org.jeecgframework.web.system.controller.core;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDelivery;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 配送方式维护
 * 
 * @author 钟杰宇
 * 
 */
@Scope("prototype")
@Controller
@RequestMapping("/deliveryController")
public class DeliveryController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(DeliveryController.class);
	private SystemService systemService;
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Autowired
	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	/**
	 * 配送方式列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "delivery")
	public ModelAndView delivery() {
		return new ModelAndView("system/delivery/deliveryList");
	}

	/**
	 * easyuiAjax表单请求
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 */
	@RequestMapping(params = "datagrid")
	public void datagrid(HttpServletRequest request,
			HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(TSDelivery.class, dataGrid);
		this.systemService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除配送信息
	 * 
	 * @param delivery
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(TSDelivery delivery, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();

		try {

			delivery = systemService.getEntity(TSDelivery.class, delivery.getId());

			systemService.delete(delivery);
			message = "配送方式: " + delivery.getTitle() + "被删除 成功";
			systemService.addLog(message, Globals.Log_Type_DEL,
					Globals.Log_Leavel_INFO);

		}catch (Exception e){
			e.printStackTrace();
			message = "删除配送方式失败";
		}

		j.setMsg(message);
		return j;
	}

	/**
	 * 添加和更新配置信息
	 * 
	 * @param delivery
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(TSDelivery delivery,HttpServletRequest request) {

		try {

			if (StringUtil.isEmpty(delivery.getId())) {
				TSDelivery pament2=systemService.findUniqueByProperty(TSDelivery.class, "title", delivery.getTitle());
				if(pament2!=null){
					message = "配送方式: " + delivery.getTitle() + "的信息已存在";
				}else{

					systemService.save(delivery);
					message = "配送方式: " + delivery.getTitle() + "被添加成功";
					systemService.addLog(message, Globals.Log_Type_INSERT,
							Globals.Log_Leavel_INFO);
				}

			}else{
				message = "配送方式: " + delivery.getTitle() + "被修改成功";
				systemService.updateEntitie(delivery);
				systemService.addLog(message, Globals.Log_Type_INSERT,
						Globals.Log_Leavel_INFO);
			}

		}catch (Exception e){
			e.printStackTrace();
			message = "配送方式设置失败！请检查数据是否完整！";
		}


		AjaxJson j = new AjaxJson();
		j.setMsg(message);
		
		return j;
	}

	/**
	 * 添加和更新支付方式页面
	 * 
	 * @param delivery
	 * @param req
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(TSDelivery delivery, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(delivery.getId())) {
			delivery = systemService.getEntity(TSDelivery.class,
					delivery.getId());
			req.setAttribute("delivery", delivery);
		}
		return new ModelAndView("system/delivery/delivery");
	}

}
